package com.zhongwang.mms.module.buy.service;

import com.zhongwang.mms.base.BaseService;
import com.zhongwang.mms.gen.entity.BReportPlan;
import com.zhongwang.mms.gen.entity.BShoppingPlan;
import com.zhongwang.mms.gen.entity.HOrderDetail;
import com.zhongwang.mms.gen.entity.POrderDetail;
import com.zhongwang.mms.gen.mapper.BShoppingPlanMapper;
import com.zhongwang.mms.module.buy.dao.BuyPlanDao;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.half.dao.HOrderDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**********************************************
 * @title: OutsourceReturnService
 * @description: 外协件确认回货业务逻辑层
 * @author: 徐一贺
 * @date: 2020/11/3 18:12
 * @versron: ver 1.0
 **********************************************
 */
@Service
public class OutsourceReturnService extends BaseService {

    @Autowired
    private BuyPlanDao buyPlanDao;
    @Autowired
    private BShoppingPlanMapper shoppingPlanMapper;
    @Autowired
    private ReportPlanDao reportPlanDao;
    @Autowired
    private HOrderDetailDao hOrderDetailDao;
    /**
     * 查询外协件回货列表
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public List<ShoppingPlanModel> findOutsourceReturnList(ShoppingPlanModel shoppingPlanModel) {
        return buyPlanDao.findOutsourceReturnList(shoppingPlanModel);
    }

    /**
     * 批量确认回货
     *
     * @param sppId 采购计划主键数组
     * @param odaId 入库记录主键数组
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int submitReturn(Integer[] sppId, Integer[] odaId) {
        for (int i = 0; i < sppId.length; i++) {
            submitReturn(sppId[i], odaId[i]);
        }
        return 1;
    }

    /**
     * 确认回货
     * 获取单价回写到提报计划表 单价用于核算
     * @param sppId 采购计划ID
     * @param odaId 入库记录ID
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int submitReturn(Integer sppId, Integer odaId) {
        BShoppingPlan plan = shoppingPlanMapper.selectByPrimaryKey(sppId);
        logService.saveLog("确认回货", "物料描述：" + plan.getSppMatDesc());
        plan.setSppOdaId(odaId);
        plan.setSppRetStatus(1);

        //确认回货条件提报计划总数等于采购计划总数即获取入库单单价，
        //否则单价为0，同时在采购计划单价和提报计划单价都记录单价，
        //提报计划还要记录SAP编码和物料描述
        //获取合报的提报计划列表
        List<BReportPlan> list = reportPlanDao.findBReportPlanBySppId(plan.getSppId());
        //获取提报计划总数
        Double totalNum = 0d;
        for (BReportPlan bReportPlan:list) {
            totalNum+=bReportPlan.getRppNum();
        }
        //如果采购数量等于提报计划总数
        if(plan.getSppNum().equals(totalNum)){
            HOrderDetail hOrderDetail = hOrderDetailDao.getHOrderDetailByPrimaryKey(odaId);
            //获取单价
            for (int i = 0; i < list.size() ; i++) {
                BReportPlan changeRppBuyPriceModel =  list.get(i);
                changeRppBuyPriceModel.setRppBuyPrice(hOrderDetail.getOdaPrice());
                changeRppBuyPriceModel.setRppConfirmReturnDate(new Date());
                reportPlanDao.updateBReportPlanRppBuyPriceAndRppConfirmReturnDateByRppId(changeRppBuyPriceModel);
                reportPlanDao.updateBReportPlanRppSapCodeAndRppBuyStockMatDesc(changeRppBuyPriceModel.getRppId(),hOrderDetail.getOdaSapCode(),hOrderDetail.getOdaMatName());
            }
            buyPlanDao.updateSppPrice(hOrderDetail.getOdaPrice(),sppId);
        }else {
            //获取单价
            for (int i = 0; i < list.size() ; i++) {
                BReportPlan changeRppBuyPriceModel =  list.get(i);
                changeRppBuyPriceModel.setRppBuyPrice(0d);
                changeRppBuyPriceModel.setRppConfirmReturnDate(new Date());
                reportPlanDao.updateBReportPlanRppBuyPriceAndRppConfirmReturnDateByRppId(changeRppBuyPriceModel);
            }
            buyPlanDao.updateSppPrice(0d,sppId);
        }
        return shoppingPlanMapper.updateByPrimaryKey(plan);
    }


    /**
     * 撤消回货确认
     *
     * @param id 采购ID
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int cancelRetrun(Integer id) {
        BShoppingPlan plan = shoppingPlanMapper.selectByPrimaryKey(id);
        List<BReportPlan> list = reportPlanDao.findBReportPlanBySppId(plan.getSppId());
        //获取单价
        for (int i = 0; i < list.size() ; i++) {
            BReportPlan changeRppBuyPriceModel =  list.get(i);
            changeRppBuyPriceModel.setRppBuyPrice(null);
            changeRppBuyPriceModel.setRppConfirmReturnDate(null);
            reportPlanDao.updateBReportPlanRppBuyPriceAndRppConfirmReturnDateByRppId(changeRppBuyPriceModel);
            reportPlanDao.updateBReportPlanRppSapCodeAndRppBuyStockMatDesc(changeRppBuyPriceModel.getRppId(),null,null);
        }
        buyPlanDao.updateSppPrice(null,id);
        logService.saveLog("撤消回货确认", "物料描述：" + plan.getSppMatDesc());
        plan.setSppOdaId(null);
        plan.setSppRetStatus(0);
        return shoppingPlanMapper.updateByPrimaryKey(plan);
    }

    /**
     * 查询入库记录
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public List<ShoppingPlanModel> findOrderDetail(ShoppingPlanModel shoppingPlanModel) {
        return buyPlanDao.findOrderDetailByOutSource(shoppingPlanModel);
    }

    /**
     * 检测 项目号 SAP物料编码 跟踪号 图号 数量
     *
     * @param sppId 采购计划主键
     * @param odaId 入库主键
     * @return int 不同返回1 相同返回0
     */
    public int checkProCodeAndTrackNo(Integer sppId, Integer odaId) {
        BShoppingPlan bShoppingPlan = buyPlanDao.getBShoppingPlanByPrimaryKey(sppId);
        List<BReportPlan> bReportPlanList = buyPlanDao.findBReportPlanByRppSppId(sppId);
        HOrderDetail hOrderDetail = buyPlanDao.getHOrderDetailByPrimaryKey(odaId);
        int result = 0;
        for (BReportPlan bReportPlan:bReportPlanList) {
            if(bShoppingPlan.getSppSapCode().equals(hOrderDetail.getOdaSapCode()) &&
                    bShoppingPlan.getSppProjectCode().equals(hOrderDetail.getOdaProCode()) &&
                    bShoppingPlan.getSppTrackCode().equals(hOrderDetail.getOdaTrackCode()) &&
                    bReportPlan.getRppDiagram().equals(hOrderDetail.getOdaDrawingCode()) &&
                    bShoppingPlan.getSppNum().doubleValue()==hOrderDetail.getOdaNum().doubleValue()){
                result = 0;
            }else {
                return 1;
            }
        }
        return result;
    }
}
